<template>
  <router-view></router-view>
  <div class="com-box" v-if="isIndex">
    <div class="content-box w-card">
      <div class="title-box">
        <span>商户信息</span>
      </div>
      <div class="search-box">
        <div class="form-item">
          <span>商户名称：</span>
          <el-input v-model="searchForm.shopName" clearable placeholder="请输入商户名称" @keypress.enter="searchHandle" />
        </div>
        <div class="form-item">
          <span>联系电话：</span>
          <el-input v-model="searchForm.contactsPhone" clearable placeholder="请输入联系电话" @keypress.enter="searchHandle" />
        </div>
        <div class="form-item">
          <span>商户类型：</span>
          <el-select style="width:100%" v-model="searchForm.shopType">
            <el-option v-for="item in shopOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </div>
      <div class="search-box" style="margin-top:20px;">
        <div class="form-item" style="width:30%;max-width:480px;">
          <span>操作时间：</span>
          <el-date-picker style="width:100%" v-model="searchForm.date" type="datetimerange" range-separator="至"
            start-placeholder="开始时间" end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="shopName" label="商户名称" align="center">
            <template v-slot="scope">
              <el-link type="primary" @click="lookDetail(scope.row)">{{ scope.row.shopName || '' }}</el-link>
            </template>
          </el-table-column>
          <el-table-column prop="shopType" label="商户类型" align="center">
            <template v-slot="scope">
              {{ scope.row.shopType == 1 ? '个人商户' : scope.row.shopType == 2 ? '企业商户' : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="contactsName" label="联系人" align="center">
            <template v-slot="scope">
              {{ scope.row.contactsName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="contactsPhone" label="联系电话" align="center">
            <template v-slot="scope">
              {{ scope.row.contactsPhone || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="shopCode" label="供应商代码" align="center">
            <template v-slot="scope">
              {{ scope.row.shopCode || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="leftFee" label="账号余额(元)" align="center">
            <template v-slot="scope">
              {{ numFormat(scope.row.leftFee/100) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="chargeTotal" label="充值总金额(元)" align="center">
            <template v-slot="scope">
              {{ numFormat(scope.row.chargeTotal/100) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="注册时间" align="center" width="180">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="orderDiscount" label="下单折扣" align="center">
            <template v-slot="scope">
              {{ scope.row.orderDiscount }}%
            </template>
          </el-table-column>
          <el-table-column prop="submitDiscount" label="提交折扣" align="center">
            <template v-slot="scope">
              {{ scope.row.submitDiscount }}%
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="150">
            <template v-slot="scope">
              <el-link style="font-size:14px;" type="primary" @click="editHandle(scope.row)">修改下单/提交折扣</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <edit-dialog ref="editDialogRef" @dialogClose="getTableData" />
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { warningDeal, getWindowWidthAndHeight, getDateTime, numFormat } from '@/utils/utils'
import editDialog from './components/editDialog'
import api from './api'
export default {
  components: {
    editDialog,
  },
  setup() {
    const route = useRoute()
    const router = useRouter()

    const editDialogRef = ref(null)

    const state = reactive({
      tableHeight: 0,
      page: 1,
      size: 10,
      total: 0,
      isIndex: true,
      searchForm: {
        shopType: 0,
      },
      shopOptions: [
        {
          value: 0,
          label: '全部商户',
        },
        {
          value: 1,
          label: '个人商户',
        },
        {
          value: 2,
          label: '企业商户',
        },
      ],
      tableData: [],
    })

    watch(() => route.matched, (newVal, oldVal) => {
      setIsIndex()
    })

    function setIsIndex() {
      if (route.matched[route.matched.length - 1].path == '/commercial') {
        state.isIndex = true
        getTableData()
      } else {
        state.isIndex = false
      }
    }

    function getTableData() {
      //获取列表数据
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: state.searchForm.date ? state.searchForm.date[0].getTime() : '',
        endTime: state.searchForm.date ? state.searchForm.date[1].getTime() : '',
      }
      api.shopQryList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        shopType: 0,
      }
      searchHandle()
    }

    function lookDetail(data) {
      router.push({ path: '/commercial/detail', query: { data: JSON.stringify(data) } })
    }

    function editHandle(data) {
      editDialogRef.value.dialogShow(data)
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      setIsIndex()
    })

    return {
      ...toRefs(state),
      editDialogRef,
      handleSizeChange,
      handleCurrentChange,
      searchHandle,
      resetHandle,
      getDateTime,
      numFormat,
      lookDetail,
      editHandle,
      getTableData,
    }
  }
}
</script>

<style lang="scss" scoped>
.com-box {
  width: 100%;
  height: 100%;
  .content-box {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      padding-bottom: 20px;
      > span {
        font-size: 16px;
        font-weight: bold;
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      gap: 20px;

      .form-item {
        width: 25%;
        max-width: 300px;
        display: flex;
        align-items: center;
        gap: 5px;

        > span {
          font-size: 14px;
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>